home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Utils / GParted Live CD / Bin / gparted-livecd-0.2.2.iso / usr_sqfs / bin / hfs < prev    next >
Encoding:
Text File  |  2005-12-28  |  9.9 KB  |  496 lines

  1. #! /bin/sh
  2. # Start hfssh ... \
  3. exec hfssh "$0" "$@"
  4.  
  5. #
  6. # hfsutils - tools for reading and writing Macintosh HFS volumes
  7. # Copyright (C) 1996-1998 Robert Leslie
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. #
  23. # $Id: hfs.tcl,v 1.9 1998/08/31 04:27:18 rob Exp $
  24. #
  25.  
  26. proc ctime {secs} {
  27.     return [clock format $secs -format "%c"]
  28. }
  29.  
  30. proc getvol {var} {
  31.     global curvol
  32.     upvar $var vol
  33.  
  34.     if {! [info exists curvol]} {
  35.     error "no volume is current"
  36.     }
  37.  
  38.     set vol $curvol
  39. }
  40.  
  41. proc hinfo {} {
  42.     getvol vol
  43.  
  44.     if {[$vol islocked]} {
  45.     set locked " (locked)"
  46.     } else {
  47.     set locked ""
  48.     }
  49.  
  50.     puts stdout "Volume name is \"[$vol vname]\"$locked"
  51.     puts stdout "Volume was created on [ctime [$vol crdate]]"
  52.     puts stdout "Volume was last modified on [ctime [$vol mddate]]"
  53.     puts stdout "Volume has [lindex [$vol size] 1] bytes free"
  54. }
  55.  
  56. proc hmount {path {partno ""}} {
  57.     global mounts curpath curvol
  58.  
  59.     set nparts [hfs nparts $path]
  60.     if {$nparts >= 0} {
  61.     puts stdout [concat "$path contains $nparts HFS"  \
  62.         [ternary {$nparts == 1} "partition" "partitions"]]
  63.     }
  64.  
  65.     if {[string length $partno] == 0} {
  66.     if {$nparts > 0} {
  67.         puts stderr "partition unspecified; selecting number 1"
  68.         set partno 1
  69.     } elseif {$nparts == -1} {
  70.         set partno 0
  71.     } else {
  72.         set partno 1
  73.     }
  74.     }
  75.  
  76.     set vol [hfs mount $path $partno]
  77.  
  78.     if {[info exists mounts($path)]} {
  79.     humount $path
  80.     }
  81.  
  82.     set curpath $path
  83.     set curvol $vol
  84.     set mounts($path) $vol
  85.  
  86.     hinfo
  87. }
  88.  
  89. proc humount {{path {}}} {
  90.     global mounts curpath curvol
  91.  
  92.     if {[string length $path] == 0} {
  93.     if {! [info exists curpath]} {
  94.         error "no volume is current"
  95.     }
  96.  
  97.     set path $curpath
  98.     } elseif {! [info exists mounts($path)]} {
  99.     error "$path not mounted"
  100.     }
  101.  
  102.     set vol $mounts($path)
  103.     unset mounts($path)
  104.  
  105.     if {[string compare $vol $curvol] == 0} {
  106.     unset curpath
  107.     unset curvol
  108.     }
  109.  
  110.     $vol umount
  111. }
  112.  
  113. proc hvol {name} {
  114.     global mounts curpath curvol
  115.  
  116.     if {[info exists mounts($name)]} {
  117.     set curpath $name
  118.     set curvol $mounts($name)
  119.     return
  120.     }
  121.  
  122.     error "unknown volume"
  123. }
  124.  
  125. proc hpwd {} {
  126.     getvol vol
  127.  
  128.     return "[join [$vol path] ":"]:"
  129. }
  130.  
  131. proc hcd {{path ""}} {
  132.     getvol vol
  133.  
  134.     set globbed [$vol glob [list $path]]
  135.     if {[llength $globbed] != 1} {
  136.     error "$path: ambiguous path"
  137.     }
  138.     set path [lindex $globbed 0]
  139.  
  140.     if {[string length $path] == 0} {
  141.     set path "[$vol vname]:"
  142.     }
  143.  
  144.     $vol cd $path
  145. }
  146.  
  147. proc timestr {secs} {
  148.     set ctime [ctime $secs]
  149.  
  150.     return "[string range $ctime 4 15][string range $ctime 19 23]"
  151. }
  152.  
  153. proc ternary {test true false} {
  154.     if {[uplevel expr $test]} {
  155.     return $true
  156.     } else {
  157.     return $false
  158.     }
  159. }
  160.  
  161. proc hdir {{path ":"}} {
  162.     getvol vol
  163.  
  164.     set globbed [$vol glob [list $path]]
  165.     if {[llength $globbed] != 1} {
  166.     error "$path: ambiguous path"
  167.     }
  168.     set path [lindex $globbed 0]
  169.  
  170.     foreach ent [$vol dir $path] {
  171.     array set item $ent
  172.  
  173.     if {$item(kind) == "directory"} {
  174.         puts stdout [format "d%s %9lu item%s               %s %s"  \
  175.             [ternary {[lsearch $item(flags) "invis"] >= 0} "i" " "]  \
  176.             $item(size)  \
  177.             [ternary {$item(size) == 1} " " "s"]  \
  178.             [timestr $item(mddate)]  \
  179.             $item(name)]
  180.     } else {
  181.         puts stdout [format "%s%s %4s/%4s %9lu %9lu %s %s"  \
  182.             [ternary {[lsearch $item(flags) "locked"] >= 0} "F" "f"]  \
  183.             [ternary {[lsearch $item(flags) "invis"] >= 0} "i" " "]  \
  184.             $item(type)  \
  185.             $item(creator)  \
  186.             $item(rsize)  \
  187.             $item(dsize)  \
  188.             [timestr $item(mddate)]  \
  189.             $item(name)]
  190.     }
  191.     }
  192. }
  193.  
  194. proc hstat {{path ":"}} {
  195.     getvol vol
  196.  
  197.     set globbed [$vol glob [list $path]]
  198.     if {[llength $globbed] != 1} {
  199.     error "$path: ambiguous path"
  200.     }
  201.     set path [lindex $globbed 0]
  202.  
  203.     array set item [$vol stat $path]
  204.  
  205.     foreach elt [lsort [array names item]] {
  206.     if {[regexp {date$} $elt]} {
  207.         set value [ctime $item($elt)]
  208.     } else {
  209.         set value $item($elt)
  210.     }
  211.  
  212.     puts stdout [format "%-10s %s" "$elt:" $value]
  213.     }
  214. }
  215.  
  216. proc hmkdir {args} {
  217.     getvol vol
  218.  
  219.     foreach arg [$vol glob $args] {
  220.     $vol mkdir $arg
  221.     }
  222. }
  223.  
  224. proc hrmdir {args} {
  225.     getvol vol
  226.  
  227.     foreach arg [$vol glob $args] {
  228.     $vol rmdir $arg
  229.     }
  230. }
  231.  
  232. proc hcreate {path {type "TEXT"} {creator "UNIX"}} {
  233.     getvol vol
  234.  
  235.     set file [$vol create $path $type $creator]
  236.     $file close
  237. }
  238.  
  239. proc htouch {args} {
  240.     getvol vol
  241.  
  242.     foreach arg [$vol glob $args] {
  243.     if [catch {$vol touch $arg}] {
  244.         hcreate $arg
  245.     }
  246.     }
  247. }
  248.  
  249. proc hdel {args} {
  250.     getvol vol
  251.  
  252.     foreach arg [$vol glob $args] {
  253.     $vol delete $arg
  254.     }
  255. }
  256.  
  257. proc hrename {src dst} {
  258.     getvol vol
  259.  
  260.     set globbed [$vol glob [list $src]]
  261.     if {[llength $globbed] != 1} {
  262.     error "$src: ambiguous path"
  263.     }
  264.     set src [lindex $globbed 0]
  265.  
  266.     $vol rename $src $dst
  267. }
  268.  
  269. proc hbless {path} {
  270.     getvol vol
  271.  
  272.     set globbed [$vol glob [list $path]]
  273.     if {[llength $globbed] != 1} {
  274.     error "$path: ambiguous path"
  275.     }
  276.     set path [lindex $globbed 0]
  277.  
  278.     $vol bless $path
  279. }
  280.  
  281. proc hcat {path} {
  282.     getvol vol
  283.  
  284.     set globbed [$vol glob [list $path]]
  285.     if {[llength $globbed] != 1} {
  286.     error "$path: ambiguous path"
  287.     }
  288.     set path [lindex $globbed 0]
  289.  
  290.     set file [$vol open $path]
  291.  
  292.     while {1} {
  293.     set buf [$file read 512]
  294.     if {[string length $buf] == 0} {
  295.         $file close
  296.         break
  297.     }
  298.  
  299.     regsub -all "\r" $buf "\n" buf
  300.  
  301.     puts -nonewline stdout $buf
  302.     }
  303. }
  304.  
  305. proc hcopyout {path {dest "."} {mode ""}} {
  306.     getvol vol
  307.  
  308.     set globbed [$vol glob [list $path]]
  309.     if {[llength $globbed] != 1} {
  310.     error "$path: ambiguous path"
  311.     }
  312.     set path [lindex $globbed 0]
  313.  
  314.     if {[string length $mode] == 0} {
  315.     array set item [$vol stat $path]
  316.  
  317.     if {$item(kind) == "directory"} {
  318.         error "can't copy whole directories"
  319.     } elseif {[regexp {^TEXT|ttro$} $item(type)]} {
  320.         set mode text
  321.     } else {
  322.         set mode macb
  323.     }
  324.     }
  325.  
  326.     $vol copyout $mode $path $dest
  327. }
  328.  
  329. proc hcopyin {path {dest ":"} {mode ""}} {
  330.     getvol vol
  331.  
  332.     set globbed [$vol glob [list $path]]
  333.     if {[llength $globbed] != 1} {
  334.     error "$path: ambiguous path"
  335.     }
  336.     set path [lindex $globbed 0]
  337.  
  338.     if {[string length $mode] == 0} {
  339.     if {[regexp {\.bin$} $path]} {
  340.         set mode macb
  341.     } elseif {[regexp {\.hqx$} $path]} {
  342.         set mode binh
  343.     } elseif {[regexp {\.(txt|c|h)$} $path]} {
  344.         set mode text
  345.     } elseif {[regexp {\.(sit|sea|cpt|tar|gz|Z|gif|jpg)$} $path]} {
  346.         set mode raw
  347.     } elseif {[catch {exec file -L $path} type] == 0 &&  \
  348.         [regexp {text} $type]} {
  349.         set mode text
  350.     } else {
  351.         set mode raw
  352.     }
  353.     }
  354.  
  355.     $vol copyin $mode $path $dest
  356. }
  357.  
  358. proc hformat {path {partno 0} {vname "Untitled"} {badblocks {}}} {
  359.     global mounts
  360.  
  361.     if {[info exists mounts($path)]} {
  362.     humount $path
  363.     }
  364.  
  365.     hfs format $path $partno $vname $badblocks
  366.  
  367.     hmount $path $partno
  368. }
  369.  
  370. ###############################################################################
  371.  
  372. proc help {{what ""}} {
  373.     if {[string length $what]} {
  374.     if {[catch {info args h$what} msg]} {
  375.         puts stdout "Sorry, no help for \"$what\""
  376.     } else {
  377.         puts stdout "$what $msg"
  378.     }
  379.     } else {
  380.     puts stdout {
  381. info        Show current volume information
  382. mount        Open a new HFS volume
  383. umount        Close an HFS volume
  384. vol        Select an open volume
  385. pwd        Show the current working directory path
  386. cd        Change the current working directory
  387. dir        Show a directory listing
  388. stat        Show details about a given path
  389. mkdir        Create a new directory
  390. rmdir        Delete an empty directory
  391. create        Create an empty file
  392. touch        Update modification date or create a file
  393. del        Delete a file
  394. rename        Rename a file, directory, or volume
  395. cat        Display file's data fork contents
  396. copyout        Copy a complete file out into the native filesystem
  397. copyin        Copy a MacBinary/BinHex/text/raw file into HFS
  398. format        Create an empty HFS volume
  399. }
  400.     }
  401. }
  402.  
  403. proc ? {args} {
  404.     eval help $args
  405. }
  406.  
  407. ###############################################################################
  408.  
  409. proc version {} {
  410.     puts stdout "[hfs version] - [hfs copyright]"
  411. }
  412.  
  413. proc license {} {
  414.     puts -nonewline stdout "\n[hfs license]"
  415. }
  416.  
  417. proc author {} {
  418.     puts stdout [hfs author]
  419. }
  420.  
  421. if {[string compare [lindex $argv 0] "--license"] == 0} {
  422.     license
  423.     exit
  424. }
  425.  
  426. version
  427.  
  428. if {[string compare [lindex $argv 0] "--version"] == 0} {
  429.     puts stdout "`$argv0 --license' for licensing information."
  430.     exit
  431. }
  432.  
  433. puts stdout "This is free software but comes with ABSOLUTELY NO WARRANTY."
  434. if {$hfs_interactive} {
  435.     puts stdout "Type `license' for details."
  436. }
  437. puts stdout ""
  438.  
  439. ###############################################################################
  440.  
  441. proc echo {args} {
  442.     puts stdout [join $args " "]
  443. }
  444.  
  445. proc quit {} {
  446.     exit
  447. }
  448.  
  449. ###############################################################################
  450.  
  451. # Apparently some shells don't grok "$@" correctly
  452. if {$argc == 1 && [string length [lindex $argv 0]] == 0} {
  453.     incr argc -1
  454.     set argv [lreplace $argv 0 0]
  455. }
  456.  
  457. if {$argc > 0} {
  458.     eval hmount $argv
  459. }
  460.  
  461. while {1} {
  462.     if {$hfs_interactive} {
  463.     puts -nonewline stdout "hfs> "
  464.     flush stdout
  465.     }
  466.  
  467.     if {[gets stdin line] == -1} {
  468.     exit
  469.     }
  470.  
  471.     while {! [info complete $line]} {
  472.     if {[gets stdin more] == -1} {
  473.         break
  474.     } else {
  475.         set line "$line$more"
  476.     }
  477.     }
  478.  
  479.     if {[string length [info procs "h[lindex $line 0]"]] > 0} {
  480.     set result [catch {eval h$line} msg]
  481.     } else {
  482.     set result [catch {eval $line} msg]
  483.     }
  484.  
  485.     if {[string length $msg] > 0} {
  486.     if {$result == 1} {
  487.         puts stdout "Error: $msg"
  488.         if {! $hfs_interactive} {
  489.         exit 1
  490.         }
  491.     } else {
  492.         puts stdout $msg
  493.     }
  494.     }
  495. }
  496.